home *** CD-ROM | disk | FTP | other *** search
- {*****************************************************************************
- This unit lets a program take control of the standard operations for New,
- GetMem, Dispose, FreeMem from the SYSTEM unit. USE it anywhere in a program's
- USES list. You must call the routine CustomHeapControl in order to grab
- control.
-
- For further information about this unit, refer to HEAP.DOC.
-
- Written 7/18/88, Kim Kokkonen, TurboPower Software.
- Compuserve ID 72457,2131
- Released to the public domain.
-
- Version 1.0
- First release.
- Version 5.0
- For consistency with 5.0 release of other heap utilities.
- *****************************************************************************}
-
- {$R-,S-,B-,F-}
-
- unit GrabHeap;
-
- interface
-
- procedure CustomHeapControl(GetPtr, FreePtr : Pointer);
- {-Give control of GetMem, New, FreeMem, Dispose to specified procedures}
-
- procedure SystemHeapControl;
- {-Restore control to the system heap routines}
-
- {===============================================================}
-
- implementation
-
- type
- Xfer = record
- Instr : Byte;
- Addr : Pointer;
- end;
- var
- P : ^Byte;
- GetMemPtr : ^Xfer;
- FreeMemPtr : ^Xfer;
- GetSave : Xfer;
- FreeSave : Xfer;
-
- procedure CustomHeapControl(GetPtr, FreePtr : Pointer);
- var
- X : Xfer;
- begin
- with X do begin
- Instr := $EA; {JMP FAR}
- Addr := GetPtr;
- GetMemPtr^ := X;
- Addr := FreePtr;
- FreeMemPtr^ := X;
- end;
- end;
-
- procedure SystemHeapControl;
- begin
- GetMemPtr^ := GetSave;
- FreeMemPtr^ := FreeSave;
- end;
-
- function FindFarProcCall : Pointer;
- {-Return pointer to far procedure called just previously}
- inline
- ($E8/$00/$00/ { call next}
- $5F/ {next: pop di}
- $0E/ { push cs}
- $07/ { pop es}
- $83/$EF/$07/ { sub di,7}
- $26/$C4/$05/ { les ax,es:[di]}
- $8C/$C2); { mov dx,es}
-
- begin
- {Find GetMem and FreeMem in SYSTEM}
- New(P);
- GetMemPtr := FindFarProcCall;
- Dispose(P);
- FreeMemPtr := FindFarProcCall;
- {Save the first 5 bytes of each routine, which will be overwritten}
- GetSave := GetMemPtr^;
- FreeSave := FreeMemPtr^;
- end.